package com.zhoug.mpchart.data;

import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.zhoug.mpchart.core.MPCombinedKey;
import com.zhoug.mpchart.core.MPConstant;
import com.zhoug.mpchart.core.MPEntryList;
import com.zhoug.mpchart.core.styleconfig.IMPCombinedStyleConfig;
import com.zhoug.mpchart.core.styleconfig.MPCombinedStyleConfig;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;

/**
 * 柱状图和曲线图 数据工厂
 *
 * @Author: zhoug
 * @Date: 2024-02-03
 * @Description:
 */
public class MPBarLineChartDataCreator {
    private IMPCombinedStyleConfig styleConfig;
    private List<String> mXLabels = new ArrayList<>();
    private MPBarChartDataCreator mBarChartDataCreator;
    private MPLineChartDataCreator mLineChartDataCreator;

    public MPBarLineChartDataCreator() {
        this(new MPCombinedStyleConfig());
    }

    public MPBarLineChartDataCreator(IMPCombinedStyleConfig styleConfig) {
        if (styleConfig != null) {
            this.styleConfig = styleConfig;
        } else {
            this.styleConfig = new MPCombinedStyleConfig();
        }
        mBarChartDataCreator=new MPBarChartDataCreator();
        mBarChartDataCreator.setStyleConfig(this.styleConfig);
        mLineChartDataCreator=new MPLineChartDataCreator();
        mLineChartDataCreator.setStyleConfig(this.styleConfig);

    }

    public void setBarDataSetValueFormatter(ValueFormatter barDataSetValueFormatter) {
        this.mBarChartDataCreator.setDataSetValueFormatter(barDataSetValueFormatter);
    }

    public void setLineDataSetValueFormatter(ValueFormatter lineDataSetValueFormatter) {
        mLineChartDataCreator.setDataSetValueFormatter(lineDataSetValueFormatter);
    }

    public void setOnBarDateSetCreateListener(OnDateSetCreateListener<BarDataSet> onBarDateSetCreateListener) {
        this.mBarChartDataCreator.setOnDateSetCreateListener(onBarDateSetCreateListener);
    }

    public void setOnLineDateSetCreateListener(OnDateSetCreateListener<LineDataSet> onLineDateSetCreateListener) {
        mLineChartDataCreator.setOnDateSetCreateListener(onLineDateSetCreateListener);
    }

    public void setStyleConfig(@NonNull MPCombinedStyleConfig styleConfig) {
        this.styleConfig = styleConfig;
        mBarChartDataCreator.setStyleConfig(this.styleConfig);
        mLineChartDataCreator.setStyleConfig(this.styleConfig);
    }

    public List<String> getXLabels() {
        return mXLabels;
    }


    public CombinedData crateCombinedData(List<HkChartData> list, List<MPCombinedKey> mpCombinedKeys) {
        //x轴名称
        if (mXLabels == null) {
            mXLabels = new ArrayList<>();
        } else {
            mXLabels.clear();
        }
        if (list != null && list.size() > 0) {
            if (mpCombinedKeys == null || mpCombinedKeys.isEmpty()) {
                mpCombinedKeys = createMPBarLineKey(list);
            }
            if (mpCombinedKeys == null || mpCombinedKeys.isEmpty()) {
                return null;
            }
            List<MPEntryList<BarEntry>> barEntries = null;   //柱状图
            List<MPEntryList<Entry>> lineEntries = null;   //曲线图
            for (MPCombinedKey mpCombinedKey : mpCombinedKeys) {
                if (mpCombinedKey.getChartType() == MPConstant.BAR_CHART) {
                    if (barEntries == null) {
                        barEntries = new ArrayList<>();
                    }
                    barEntries.add(new MPEntryList<>(mpCombinedKey.getKeys(), mpCombinedKey.getAxisDependency()));
                } else if (mpCombinedKey.getChartType() == MPConstant.LINE_CHART) {
                    if (lineEntries == null) {
                        lineEntries = new ArrayList<>();
                    }
                    lineEntries.add(new MPEntryList<>(mpCombinedKey.getKeys(), mpCombinedKey.getAxisDependency()));
                }
            }
            for (int i = 0; i < list.size(); i++) {
                int x = i + 1;
                HkChartData hkChartData = list.get(i);
                String name = hkChartData.getName();
                mXLabels.add(name);
                //柱状图
                if (barEntries != null) {
                    MPBarChartDataCreator.initMPEntryList(barEntries, x, hkChartData);
                }
                //曲线图
                if (lineEntries != null) {
                    MPLineChartDataCreator.initMPEntryList(lineEntries, x, hkChartData);
                }
            }
            CombinedData combinedData = new CombinedData();
            //柱状图
            if (barEntries != null) {
                BarData barData = mBarChartDataCreator.createBarData(barEntries);
                if (barData != null) {
                    combinedData.setData(barData);
                }
            }
            //曲线图
            if (lineEntries != null) {
                LineData lineData = mLineChartDataCreator.createLineData(lineEntries);
                if (lineData != null) {
                    combinedData.setData(lineData);
                }
            }
            return combinedData;
        }
        return null;
    }

    /**
     * 根据配置创建默认的 List<MPBarLineKey>
     *
     * @param list
     * @return
     */
    protected List<MPCombinedKey> createMPBarLineKey(List<HkChartData> list) {
        String[] keys = HkChartData.getKeys(list);
        if (keys != null && keys.length > 0) {
            List<MPCombinedKey> result = new ArrayList<>();
            if (styleConfig.isStackBar() && styleConfig.getDefaultChartType() == MPConstant.BAR_CHART) {
                result.add(new MPCombinedKey(keys));
            } else {
                for (String key : keys) {
                    if (styleConfig.getDefaultChartType() == MPConstant.BAR_CHART) {
                        result.add(new MPCombinedKey(new String[]{key}));
                    } else {
                        result.add(new MPCombinedKey(key, MPConstant.LINE_CHART, MPConstant.LEFT));
                    }
                }
            }
            return result;
        }
        return null;
    }


}
